package com.wlstock.funi.ui;

import android.os.Bundle;

import com.em.baselibrary.BaseActivity;
import com.em.widget.view.SelectPopUpWindow;
import com.em.widget.view.SelectPopUpWindow.onPopViewListener;
import com.em.widget.view.SelectPopUpWindow.onDeleteViewListener;
import com.wlstock.funi.R;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.RequestEvent;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;
import com.wlstock.funi.utils.ToastUtils;

import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

public abstract class FundBaseActivity extends BaseActivity {
	
	private SelectPopUpWindow selectPopUpWindow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//这个在每个Activity中单独注册比较好
		EventBus.getDefault().register(this);


	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}
	
	protected void initView() {
		
	}
	
	public void onEventMainThread(RequestEvent event) {  
		if (event.getWhat() == EventBusConstants.EVENT_REQUEST_FAILE) {
			//错误的统一处理
			handleErrResponse(event);
		} else if (event.getWhat() == EventBusConstants.EVENT_REFRESH_LOGIN_SESSION_SUCCESS) {
			ToastUtils.showMiddleMsg(this, event.getResponse());
		} else {
			handleResponse(event);
		}
	}
	
	public void handleResponse(RequestEvent event){
		if (event.getWhat() == EventBusConstants.EVENT_REFRESH_LOGIN_SESSION_SUCCESS) {
			ToastUtils.showMiddleMsg(this, event.getResponse());
		}
	};
	
	@SuppressWarnings("deprecation")
	public void handleErrResponse(RequestEvent event){
		NotiUtils.showSimpleNoti(this, NotiIdGenerator.ID_SERVER_ERROR, "服务器异常", "异常信息:"+event.getError().toString(), R.drawable.ic_exception);
	}
	
	public void showPopUpView(List<Map<String, Object>> popList,onPopViewListener popListener){
		if (selectPopUpWindow == null) {
			selectPopUpWindow = new SelectPopUpWindow(this, popList);
			selectPopUpWindow.popListener = popListener;
		}
		if (!selectPopUpWindow.isShowing()) {
			selectPopUpWindow.showDropDown(findViewById(R.id.layout_top_but));	
		}
	}

	public void showPopUpView(List<Map<String, Object>> popList,onPopViewListener popListener,onDeleteViewListener deleteListener){
		this.showPopUpView(popList,popListener);
		selectPopUpWindow.deleteListener = deleteListener;
	}

	public void invalidPopView(List<Map<String, Object>> popList){
		onDeleteViewListener tmpDelListener = null;
		onPopViewListener tmpPopListener = null;
		if (selectPopUpWindow!=null){
			tmpDelListener = selectPopUpWindow.deleteListener;
			tmpPopListener  = selectPopUpWindow.popListener;
			if (selectPopUpWindow.isShowing()){
				selectPopUpWindow.dismiss();
			}
		}
		selectPopUpWindow = new SelectPopUpWindow(this, popList);
		selectPopUpWindow.popListener = tmpPopListener;
		selectPopUpWindow.deleteListener = tmpDelListener;
		if (!selectPopUpWindow.isShowing()) {
			selectPopUpWindow.showDropDown(findViewById(R.id.layout_top_but));
		}
	}
	
}
